/**
 * DSM - Distributed Search Manager
 * Developed by Milspec Research International Pty Ltd
 * $Author: bslattery $
 * $Revision: 1.8 $
 * $Date: 2007/09/13 04:08:45 $
 * (c)Copyright 2004
 * education.au limited
 * DEST
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the names education.au limited, DEST nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * To the extent permitted by law, the copyright owners of this software and
 * its contributors:
 * (i) exclude all warranties in relation to the software; and
 * (ii) exclude liability for all losses, costs, expenses and damages arising
 * in any way from the use of the software whether arising from or in
 * relation to breach of contract, negligence or any other tort, in equity
 * or otherwise. If the software is in breach of a warranty which is
 * implied by law, the copyright owners and contributors liability is
 * limited to the replacement of the software.
 *
 * @author	gsingh
 */

package au.edu.educationau.opensource.dsm.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import au.edu.educationau.opensource.dsm.DeusExMachina;
import au.edu.educationau.opensource.dsm.obj.SearchCriteria;
import au.edu.educationau.opensource.dsm.obj.SearchResultSet;
import au.edu.educationau.opensource.dsm.util.EducationAuUtils;
import au.edu.educationau.opensource.dsm.util.StringUtils;
import au.edu.educationau.opensource.dsm.worker.SearchJob;

/**
 * This is the controller servlet for the front end DSM search page (jsp based)
 */

public class DSM extends HttpServlet {

	/** Servlet Context */
	protected ServletContext servletContext;

	/**
	 * Servlet init
	 * 
	 * @param config
	 * @exception ServletException
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		this.servletContext = config.getServletContext();
		System.setProperty("DSM4-dsm4_properties_dir", getServletContext().getRealPath(config.getInitParameter("dsm4_properties_dir")));
		System.setProperty("DSM4-dsm4_castor_mapping_file", getServletContext().getRealPath(config.getInitParameter("dsm4_castor_mapping_file")));
		System.setProperty("DSM4-dsm4_logging_level", config.getInitParameter("dsm4_logging_level"));
		System.setProperty("DSM4-dsm4_dict_file", getServletContext().getRealPath(config.getInitParameter("dsm4_dict_file")));
		System.setProperty("DSM4-dsm4_phonet_file", getServletContext().getRealPath(config.getInitParameter("dsm4_phonet_file")));
		DeusExMachina.init();
	}

	/** Destroy objects worthy of destruction */
	public void destroy() {
		DeusExMachina.destroy();
	}

	/**
	 * Service method works with both GET and POST
	 * 
	 * @param request
	 * @param response
	 * @exception ServletException
	 * @exception IOException
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		long token = EducationAuUtils.getLong(request.getParameter("token"), DeusExMachina.generateToken());
		String fwdJsp = StringUtils.checkString(request.getParameter("fwdJsp"), "result.jsp");

		// check if the job exists
		if (DeusExMachina._SearchManagerService().jobExists(token)) {
			SearchJob job = DeusExMachina._SearchManagerService().getJob(token);
			SearchCriteria oldCriteria = job.getSearchCriteria();
			DeusExMachina._SearchManagerService().monitorJob(token);
			request.setAttribute("criteria", oldCriteria);
		} else {
			// Build criteria
			SearchCriteria criteria = DeusExMachina._SearchManagerService().buildSearchCriteria(request, token);
			// generate a new search
			request.setAttribute("criteria", criteria);
			DeusExMachina._SearchManagerService().search(criteria);
		}
		DeusExMachina.logSearchToDatabase(DeusExMachina._SearchManagerService().getJob(token), request);

		SearchResultSet set = DeusExMachina._SearchResultService().getResults(token);
		request.setAttribute("resultset", set);

		RequestDispatcher rdisp = getServletContext().getRequestDispatcher("/" + fwdJsp);
		rdisp.forward(request, response);
	}

}

// -- EOF

